﻿Public Class ModelDetailListPanel
	Inherits System.Windows.Forms.Panel
	
	Private _modelDetail As ModelDetail
	Private _label As Label 
	Private _picture As PictureBox
	Private _service as WallpaperService
	Private WithEvents _prevButton As Button
	Private WithEvents _nextButton As Button 
	Private _position as Integer = 0
	
	Private Sub AddPosition(value As Integer)
		'TODO: Rechnung beim ersten klick läuft noch falsch
		_position += value
		If _position < 0 Then
			_position = _modelDetail.Modellist.Count - 1
		End If
		If _position > _modelDetail.Modellist.count - 1 Then
			_position = 0
		End If
	End Sub
	
	Public Sub New(md as ModelDetail)
		MyBase.New()
		
		_service = ServiceManager.GetServiceManager.GetWallpaperService 
		
		_modelDetail = md
		
		_label = New System.Windows.Forms.Label 
		_label.AutoSize = True 
		_label.Text = _modelDetail.DisplayName
		_label.Location = New Point(8, 5)
		_label.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline),System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0,Byte))
		Controls.Add(_label)
		
		_picture = New PictureBox 
		_picture.Location = New Point(50, 25)
		_picture.Size = New Size(120, 120)
		_picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom 
		_picture.ImageLocation = _modelDetail.Preview 
		_picture.BorderStyle = BorderStyle.FixedSingle
		_picture.Refresh()
		Controls.Add(_picture)
		
		_prevButton = New Button 
		_prevButton.Text = "<" & vbnewline & "<"
		_prevButton.Size = New Size(20, 120)
		_prevButton.Location = New Point(25, 25)
		_prevButton.FlatStyle = FlatStyle.Popup 
		addhandler _prevButton.Click, addressof PrevButtonClick_EventHandler
		Controls.Add(_prevButton)
		
		_nextButton = New Button 
		_nextButton.Text = ">" & vbnewline & ">"
		_nextButton.Size = New Size(20, 120)
		_nextButton.Location = New Point(175, 25)
		_nextButton.FlatStyle = FlatStyle.Popup 
		addhandler _nextButton.Click, addressof NextButtonClick_EventHandler
		Controls.Add(_nextButton)
		
		MyBase.Size = New Size(270, 150)
		MyBase.BorderStyle = BorderStyle.None 
		MyBase.BackColor = Color.White
		MyBase.Visible = True 
		
	End Sub
	
	Private Sub NextButtonClick_EventHandler(sender As Object, e As EventArgs)
		LoadImages()
		AddPosition(1)
		_picture.ImageLocation = Configuration.GetObject.SkinsPath & "\" & _modelDetail.Modellist.Item(_position).Filename
		_modelDetail.Modellist(_position).AddListener(MdiMain._ipf)
		_modelDetail.Modellist(_position).AddListener(MdiMain._pf)
		_modelDetail.Modellist(_position).notifyChanges()
		_picture.Refresh()
	End Sub
	
	Private Sub PrevButtonClick_EventHandler(sender As Object, e As EventArgs)
		LoadImages()
		AddPosition(-1)
		_picture.ImageLocation = Configuration.GetObject.SkinsPath & "\" & _modelDetail.Modellist.Item(_position).Filename
		_modelDetail.Modellist(_position).AddListener(MdiMain._ipf)
		_modelDetail.Modellist(_position).AddListener(MdiMain._pf)
		_modelDetail.Modellist(_position).notifyChanges()
		_picture.Refresh()
	End Sub
	
	Private Sub LoadImages()
		me.Cursor = Cursors.WaitCursor
		If _modelDetail.Modellist.Count = 0 Then
			_modelDetail.Modellist = new List(Of Wallpaper)
			_modelDetail.Modellist = _service.GetWallpaperByName(_modelDetail.Name)
		End If
		me.Cursor = Cursors.Default
	End Sub

End Class
